package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.entity.MarketReturnOrder
import com.zxy.supplier_system.server.service.MarketReturnOrderService
import com.zxy.supplier_system.server.utils.IntIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal
import java.time.LocalDate
import java.time.OffsetDateTime

@RestController
@RequestMapping("markets/returnOrders")
class MarketReturnOrderController(
    private val marketReturnOrderService: MarketReturnOrderService,
) {

    class QueryMarketReturnOrderRequest(
        val marketIds: List<Int> = emptyList(),
        val startDate: LocalDate?,
        val endDate: LocalDate?,
        val createdByIds: List<Int> = emptyList(),
        val status: MarketReturnOrder.Status?,
    )

    @HasAuthority(Authority.卖场_退货单_查询)
    @GetMapping
    fun queryMarketReturnOrder(queryMarketReturnOrderRequest: QueryMarketReturnOrderRequest, pageable: Pageable):Page<QueryMarketReturnOrderResponse> {
        return this.marketReturnOrderService.queryMarketReturnOrder(queryMarketReturnOrderRequest,pageable)
    }

    data class QueryMarketReturnOrderResponse(
        val id:Int,
        val createdBy: IntIdAndName,
        val createdDateTime: OffsetDateTime,
        val lastModifiedDateTime:OffsetDateTime?,
        val goodsKindAmount:Int,
        val market:IntIdAndName,
        val status: MarketReturnOrder.Status,
    )

    data class UpdateOrderGoodsRequest(
        val goodsList:List<OrderGoodsItem>
    )

    data class OrderGoodsItem(
        val id:Int,
        val amount:Int
    )

    @HasAuthority(Authority.卖场_退货单_下单)
    @PatchMapping("{id}/goods")
    fun updateOrderGoods(@RequestBody updateOrderGoodsRequest: UpdateOrderGoodsRequest, @PathVariable id: Int,
    ){
        return this.marketReturnOrderService.updateOrderGoods(id,updateOrderGoodsRequest)
    }

    @HasAuthority(Authority.卖场_退货单_查询)
    @GetMapping("{id}")
    fun getMarketReturnOrderDetail(@PathVariable id: Int, includeItems:Boolean = false):MarketReturnOrderDetail{
        return this.marketReturnOrderService.getMarketReturnOrderDetail(id,includeItems)
    }

    data class MarketReturnOrderDetail(
        val id:Int,
        val createdDateTime: OffsetDateTime,
        val createdBy: IntIdAndName,
        val market:IntIdAndName,
        val items:List<MarketReturnOrderItemResponse>,
        val submittedDateTime:OffsetDateTime?,
        val validatedDateTime: OffsetDateTime?,
    )

    data class MarketReturnOrderItemResponse(
        val id:Int,
        val name: String,
        val barcode:String,
        val price: BigDecimal,
        val amount:Int,
        val createdDateTime: OffsetDateTime,
        val lastModifiedDateTime: OffsetDateTime?,
        val brand:IntIdAndName?,
        val category:IntIdAndName?,
        val factory:IntIdAndName?,
        val amountInOrder:Int,
        val boxSpecification:String?
    )

    @HasAuthority(Authority.卖场_退货单_查询)
    @GetMapping("createdBys", params = ["projection=summary"])
    fun getMarketReturnOrderCreatedEmployees():List<IntIdAndName>{
        return this.marketReturnOrderService.getAllEmployees()
    }


    @HasAuthority(Authority.卖场_退货单_下单)
    @PatchMapping("{id}/submit")
    fun submitOrder(@PathVariable id: Int ){
        return this.marketReturnOrderService.submitMarketReturnOrder(id)
    }

    @HasAuthority(Authority.卖场_退货单_验收)
    @PatchMapping("{id}/validate")
    fun validate(@PathVariable id: Int ):Int{
        return this.marketReturnOrderService.validate(id).id
    }

    @DeleteMapping("{id}")
    fun deleteOrder(@PathVariable id: Int ){
        return this.marketReturnOrderService.delete(id)
    }

}
